const router = require('koa-router')()
const path = require('path')
const db = require(path.resolve('core/database/db.js'))
const loginController = require(path.resolve('core/controllers/login_controller.js'))
const responseWrapper = require(path.resolve('core/tool/response.js'))

router.get('/login', async (ctx, next) => {
  try {
    const results = await loginController.login(ctx.query.account,ctx.query.password)
    if(results.length>0){
      const first = results[0];
      ctx.body = responseWrapper(200, first, null)
    }
    else{
      ctx.body = responseWrapper(201, null, '用户未注册')
    }
    
  } catch (error) {
    console.log(error);
    ctx.body = responseWrapper(201, null, error)
  }
})

router.get('/register', async (ctx, next) => {
  try {
    const results = await loginController.register(ctx.query.account, ctx.query.password, ctx.query.nickname)
    ctx.body = responseWrapper(200, results, null)
    
  } catch (error) {
    ctx.body = responseWrapper(201, null, error)
  }
  
  
})


// router.get('/string', async (ctx, next) => {
//   ctx.body = 'koa2 string'
// })

// router.get('/json', async (ctx, next) => {
//   ctx.body = {
//     title: 'koa2 json'
//   }
// })

module.exports = router
